package com.hospitalsystem.controller;

import com.hospitalsystem.entity.Drug;
import com.hospitalsystem.service.DrugService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Drug)表控制层
 *
 * @author makejava
 * @since 2024-12-28 19:32:38
 */
@RestController
@RequestMapping("drug")
public class DrugController {
    /**
     * 服务对象
     */
    @Resource
    private DrugService drugService;

    /**
     * 分页查询
     * @param size
     * @param page
     * @return 查询结果
     */
    @GetMapping("/getAllDrugByPage")
    public ResponseEntity<Page<Drug>> queryByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
            PageRequest pageRequest = PageRequest.of(page, size);
            return ResponseEntity.ok(this.drugService.queryByPage(pageRequest));
    }

    /**
     * 查询所有
     *
     * @return 查询结果
     */
    @GetMapping("/getAllDrug")
    public ResponseEntity<List<Drug>> findAll() {
            List<Drug> allDrugs = drugService.findAll();
            return ResponseEntity.ok(allDrugs);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param drugId
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Drug> queryById(@PathVariable("id")  String drugId) {
        return ResponseEntity.ok(this.drugService.queryById(drugId));
    }

    /**
     * 新增数据
     *
     * @param drug 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Drug> add(Drug drug) {
        return ResponseEntity.ok(this.drugService.insert(drug));
    }

    /**
     * 编辑数据
     *
     * @param drug 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Drug> edit(Drug drug) {
        return ResponseEntity.ok(this.drugService.update(drug));
    }

    /**
     * 删除数据
     *
     * @param drugId
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(String drugId) {
        return ResponseEntity.ok(this.drugService.deleteById(drugId));
    }

}

